package com.springboot.service.impl;

import com.springboot.dto.OrderDoctorList;
import com.springboot.dto.PatientOrderInfo;
import com.springboot.mapper.*;
import com.springboot.model.*;
import com.springboot.service.PatientMyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
@Service
public class PatientMyOrderServiceImpl implements PatientMyOrderService {
    @Autowired
    OrderFormMapper orderFormMapper;

    @Autowired
    DoctorInfoFormMapper doctorInfoFormMapper;

    @Autowired
    DoctorFieldFormMapper doctorFieldFormMapper;

    @Autowired
    UserLoginFormMapper userLoginFormMapper;

    @Autowired
    PatientInfoFormMapper patientInfoFormMapper;

    @Autowired
    DoctorAppointSetMapper doctorAppointSetMapper;


    @Override
    public ArrayList<OrderDoctorList> showOrderDoctors(Integer PatientId) {
        ArrayList<OrderDoctorList> orderDoctorLists = new ArrayList<OrderDoctorList>();
        //获得患者名字
        UserLoginForm userLoginForm = userLoginFormMapper.selectByPrimaryKey(PatientId);
        String PatientName = userLoginForm.getUserName();

        //从获得该患者的预约表，从该预约表查找对应的医生信息
        ArrayList<OrderForm> orderForms = orderFormMapper.selectByPatientId(PatientId);
        System.out.println(orderForms.toString());
        for(int i=0;i<orderForms.size();i++)
        {
            OrderDoctorList orderDoctorList = new OrderDoctorList();
            orderDoctorList.setPatientName(PatientName);
            OrderForm orderForm = orderForms.get(i);
            Integer doctorId = orderForm.getDoctorId();
            DoctorInfoForm doctorInfoForm = new DoctorInfoForm();
            try {
                doctorInfoForm = doctorInfoFormMapper.selectByPrimaryKey(doctorId);
                String doctorName = doctorInfoForm.getDoctorName();
                String doctorRank = doctorInfoForm.getDoctorRank();
                orderDoctorList.setDoctorName(doctorName);
                orderDoctorList.setDoctorRank(doctorRank);
                orderDoctorList.setOrderForm(orderForm);
                String doctorTel = doctorInfoForm.getDoctorTel();
                UserLoginForm userLoginForm1 = userLoginFormMapper.selectByTel(doctorTel);
                orderDoctorList.setDoctorPhoto(userLoginForm1.getLogoLink());
                //获得该医生擅长的科室
                ArrayList<DoctorFieldForm> doctorFieldForms = doctorFieldFormMapper.selectByDoctorId(doctorId);
                ArrayList<String> filed = new ArrayList<>();
                for(int j=0;j<doctorFieldForms.size();j++)
                {
                    DoctorFieldForm doctorFieldForm = doctorFieldForms.get(j);
                    filed.add(doctorFieldForm.getDoctorField());
                }
                orderDoctorList.setDoctorField(filed);
                orderDoctorLists.add(orderDoctorList);
            }
            catch (Exception e)
            {
                System.out.println("医生id:"+doctorId+"查找为空");
            }
        }
        return orderDoctorLists;
    }

    //患者在就诊记录点击某次预约时，展示就诊信息
    @Override
    public PatientInfoForm showMedicalInfoByBookId(Integer bookId) {
        PatientInfoForm patientInfoForm = new PatientInfoForm();
        try {
            patientInfoForm = patientInfoFormMapper.selectByPrimaryKey(bookId);
        }
        catch (Exception e)
        {
            System.out.println("就诊记录点击某次预约时，bookId="+bookId+"查找失败");
        }

        return patientInfoForm;
    }

    /**
     * 患者取消预约
     * @param bookId
     * @return

     * @date 2020-12-20
     */
    @Override
    public int cancelOrderByBookId(Integer bookId) {
        int result = 1;
        OrderForm orderForm = new OrderForm();
        try {
            orderForm = orderFormMapper.selectByPrimaryKey(bookId);
            orderFormMapper.deleteByPrimaryKey(bookId);
            patientInfoFormMapper.deleteByPrimaryKey(bookId);
        }
        catch (Exception e)
        {
            result=0;
            System.out.println("PatientMyOrderServiceImpl-cancelOrderByBookId:");
            System.out.println("根据bookId查找或删除预约失败");
        }

        DoctorAppointSetKey doctorAppointSetKey = new DoctorAppointSetKey();
        try {
            doctorAppointSetKey = new DoctorAppointSetKey(orderForm.getDoctorId(), orderForm.getBootDate());
            DoctorAppointSet doctorAppointSet = doctorAppointSetMapper.selectByPrimaryKey(doctorAppointSetKey);
            if(orderForm.getBootTime().equals("上午"))
            {
                Integer bookNum = doctorAppointSet.getMorningNum()+1;
                doctorAppointSet.setMorningNum(bookNum);
            }
            else {
                Integer bookNum = doctorAppointSet.getAfternoonNum()+1;
                doctorAppointSet.setAfternoonNum(bookNum);
            }
            doctorAppointSetMapper.updateByPrimaryKey(doctorAppointSet);
        }
        catch (Exception e)
        {
            result=0;
            System.out.println("PatientMyOrderServiceImpl-cancelOrderByBookId:");
            System.out.println("查找或更新doctorAppointSet失败");
        }
        return result;
    }
}
